﻿// 09520460: Trương Hoàng An
// 09520553: Vương Kim Loan
// 09520555: Phạm Hoàng Long

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            btnThuocTinh.Enabled = false;
            btnPth.Enabled = false;
            btnTim.Enabled = false;
            btnReset.Enabled = false;
            txtBaoDong.Enabled = false;
            txtKetQua.Enabled = false;
            txtLeft.Enabled = false;
            txtRight.Enabled = false;
        }

        
        BaoDong A;
        BaoDong check;
        
        BaoDong result;

        private void btnThuocTinh_Click(object sender, EventArgs e)
        {
            A = new BaoDong();
            A.AddChuoi(txtThuocTinh.Text);
            btnThuocTinh.Enabled=false;
            txtThuocTinh.Enabled=false;
            txtLeft.Enabled=true;
        }

        private void txtThuocTinh_TextChanged(object sender, EventArgs e)
        {
           check = new BaoDong();
           check.AddChuoi(txtThuocTinh.Text);
            if (Regex.IsMatch(txtThuocTinh.Text, "^[A-Z]*[A-Z]*$") &&check.CheckLap() == false && txtThuocTinh.Text != "")
                btnThuocTinh.Enabled = true;
            else
                btnThuocTinh.Enabled = false;

        }

        private void txtLeft_TextChanged(object sender, EventArgs e)
        {
            check = new BaoDong();
            check.AddChuoi(txtLeft.Text);
            if (Regex.IsMatch(txtLeft.Text, "^[A-Z]*[A-Z]*$") && check.CheckLap() == false && A.KiemTra(check) == true)
            {
                txtRight.Enabled = true;
            }
            else
            {
                txtRight.Text = "";
                txtRight.Enabled = false;                
            }
        }

        private void txtRight_TextChanged(object sender, EventArgs e)
        {
            check = new BaoDong();
            check.AddChuoi(txtRight.Text);
            if (Regex.IsMatch(txtRight.Text, "^[A-Z]*[A-Z]*$") && check.CheckLap() == false && A.KiemTra(check) == true)
                btnPth.Enabled = true;
            else
                btnPth.Enabled = false;
        }
        list B = new list();
        public char[] PTH;
        public int sopth = 0;
        private void btnPth_Click(object sender, EventArgs e)
        {
            B.addve(txtLeft.Text, txtRight.Text);
            txtBaoDong.Enabled = true;
            btnPth.Enabled = false;
            string temp;
            temp = " "+ txtLeft.Text + "->" + txtRight.Text+";";
            lblPth.Text = lblPth.Text + temp;
            txtRight.Text = "";
            txtLeft.Text = "";
            txtBaoDong.Focus();
            
        }
        BaoDong KT;
        private void txtBaoDong_TextChanged(object sender, EventArgs e)
        {
            KT = new BaoDong();
            KT.AddChuoi(txtBaoDong.Text);
            if ((txtBaoDong.Text == "") || KT.CheckLap() == true || (A.KiemTra(KT) == false))
                btnTim.Enabled = false;
            else
                btnTim.Enabled = true;
        }
        BaoDong BD;
        private void btnTim_Click(object sender, EventArgs e)
        {
            btnReset.Enabled = true;
            BD = new BaoDong();
            result = new BaoDong();
            txtKetQua.Text = "";
            BD.AddChuoi(txtBaoDong.Text);
            result = BD;
            for (int i = 0; i < B.count; i++)
                if (BD.KiemTra(B.L[i].VeTrai))
                    result.AddTT(B.L[i].VePhai);
            txtKetQua.Text = result.ganchuoi();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Reset thiệt không?", "Reset?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Hide();
                frmMain form = new frmMain();
                form.Show();
                
            }
            else
                return;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Thoát hả :( ?", "Tạm biệt", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
                MessageBox.Show("Bye bye");              
            else
                e.Cancel=true;
        }

        
    }
}
